import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/Login.vue' // 你的登录组件路径
import Register from '../components/Register.vue' // 新建的注册组件
import MainLayout from '../components/MainLayout.vue'
import WordEditor from '../components/WordEditor.vue'
import ExcelEditor from '../components/ExcelEditor.vue'
import DrawingBoard from '../components/DrawingBoard.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  // 可以添加重定向
  {
    path: '/',
    redirect: '/login'
  },

  {
    path: '/mainLayout',
    component: MainLayout,
    children: [
      {
        path: '/word',
        name: 'Word',
        component: WordEditor
      },
      {
        path: '/excel',
        name: 'Excel',
        component: ExcelEditor
      },
      {
        path: '/draw',
        component: DrawingBoard
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token"); // 修正获取 token 的键
  if (token || to.path == "/login"||to.path == "/register") { // 修正路径比较的逻辑
    next();
  } else {
    next("/login");
  }
})

export default router